﻿using FastCloud.Base;
using FastCloud.DMP.DB;
using FastCloud.DMP.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.DMP
{
    [Route("frame/dmp/column")]
    public class ColumnController : BaseController
    {

        [AutoWired]
        private DB_Column DB_Column;
        [AutoWired]
        private DB_DMP DB_DMP;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/DMP/Column/Column_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/DMP/Column/Column_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/DMP/Column/Column_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ColumnName = ValueConvert.ConvertToString(GetFormParam("ColumnName"));
            string PhysicalColumnName = ValueConvert.ConvertToString(GetFormParam("PhysicalColumnName"));
            string TableGuid = ValueConvert.ConvertToString(GetFormParam("TableGuid"));
            string sql = "SystemColumn=@SystemColumn";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            ParamList.Add(new ParamEntity() { ParamName = "SystemColumn", ParamValue = (int)FastCloud.Base.Enum.DMP.SystemColumn.非系统字段 });
            if (!string.IsNullOrEmpty(ColumnName))
            {
                sql += " and ColumnName like @ColumnName";
                ParamList.Add(new ParamEntity() { ParamName = "ColumnName", ParamValue = "%" + ColumnName + "%" });
            }
            if (!string.IsNullOrEmpty(PhysicalColumnName))
            {
                sql += " and PhysicalColumnName like @PhysicalColumnName";
                ParamList.Add(new ParamEntity() { ParamName = "PhysicalColumnName", ParamValue = "%" + PhysicalColumnName + "%" });
            }
            if (!string.IsNullOrEmpty(TableGuid))
            {
                sql += " and TableGuid = @TableGuid";
                ParamList.Add(new ParamEntity() { ParamName = "TableGuid", ParamValue = TableGuid });
            }

            return Result.OK(DB_Column.SelectPage("*", sql, "SortNum desc,AddDate", ParamList));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Column Entity)
        {
            if (!DB_Column.ExistColumn(Entity.TableGuid,Entity.PhysicalColumnName))
            {
                Entity.SystemColumn = (int)FastCloud.Base.Enum.DMP.SystemColumn.非系统字段;
                DB_DMP.InitColumnDataBaseStruct(Entity);
                DB_Column.Insert(Entity);
                return Result.OK();
            }
            else
            {
                return Result.Error("数据列已存在");
            }

        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Column Entity)
        {
            DB_DMP.DestoryColumnDataBaseStruct(Entity.ColumnGuid);
            DB_Column.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ColumnGuid = ValueConvert.ConvertToString(GetFormParam("ColumnGuid"));
            return Result.OK(DB_Column.SelectOne(ColumnGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyIDList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (string ColumnGuid in KeyIDList)
            {
                DB_DMP.DestoryColumnDataBaseStruct(ColumnGuid);
            }
            DB_Column.DeleteBatch(KeyIDList);
            return Result.OK();
        }
    }
}
